<?php
class C_Controller
{	
	protected $_request = null;
	protected $_response = null;
	protected $_router = null;
	protected $_dispatcher = null;
	public static $_instance;
	private $_applicationRoot;
	private function __construct()
	{	
		
	}
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	public function setApplicationRoot($applicationRoot)
	{
		$this->_applicationRoot = $applicationRoot;
		Zend_Registry::set("applicationRoot", $applicationRoot);
		return $this;
	}
	public static function Run()
	{
		self::getInstance()->dispatch();
	}
	public function dispatch()
	{	
		$zend_config = Zend_Registry::get("zend_config");
		$this->_request = new C_Request($this->_applicationRoot);
		$rewrite = new C_Rewrite($this->_request);
		$rewrite->addRoute('Regex' ,new C_Router_Regex());
		$rewrite->addRoute('Route' ,new C_Router_Route());
		
		
		
		$rewrite->router();
		$this->_response = new C_Response($rewrite);
		$_dispatcher = new C_Dispatcher($this->_request, $this->_response);
		
		try
		{
			ob_start();
			$_dispatcher->dispatch();
			$this->_response->appendBody(ob_get_clean());
			$this->_response->sendResponse();
			//echo $this->_response;
		}
		catch (Exception $e)
		{
			if($e instanceof Zend_Db_Exception)
			{
				echo "Zend_Db_Exception";
			}
			elseif($e instanceof Zend_Exception)
			{
				echo "Zend_Exception";
			}
			else 
			{
				echo "other Exception";
			}
			echo $e->getMessage();

		}
		
		
	}
}

?>